package com.campusflea;

/**
 * Created by Administrator on 2017/6/7.
 */

import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

import io.rong.imkit.fragment.ConversationListFragment;
import io.rong.imlib.model.Conversation;

public class HomeActivity extends FragmentActivity {


    private ViewPager mViewPager;
    private FragmentPagerAdapter mFragmentPagerAdapter;//将tab页面持久在内存中
    private Fragment mConversationList;
    private Fragment mConversationFragment = null;
    private List<Fragment> mFragment = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        mConversationList = initConversationList();//获取融云会话列表的对象
        mViewPager = (ViewPager) findViewById(R.id.viewpager);

        mFragment.add(HomeFragment.getInstance());//加入第一页
//        mFragment.add(mConversationList);//聚合会话列表
        mFragment.add(FriendFragment.getInstance());//加入第三页

        //配置ViewPager的适配器
        mFragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragment.get(position);
            }

            @Override
            public int getCount() {
                return mFragment.size();
            }
        };
        mViewPager.setAdapter(mFragmentPagerAdapter);

    }

    private Fragment initConversationList() {

        /**
         * appendQueryParameter对具体的会话列表做展示
         */
        if (mConversationFragment == null) {
            ConversationListFragment listFragment = new ConversationListFragment();
            Uri uri = Uri.parse("rong://" + getApplicationInfo().packageName).buildUpon()
                    .appendPath("conversationList")
                    .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false")//设置私聊会话是否聚合显示
                    .appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "true")
                    .appendQueryParameter(Conversation.ConversationType.DISCUSSION.getName(), "false")//设置私聊会话是否聚合显示
                    .appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "false")//设置私聊会是否聚合显示
                    .build();
            listFragment.setUri(uri);
            return listFragment;
        } else {
            return mConversationFragment;
        }
    }
}
//public class HomeActivity extends FragmentActivity
//{
//
//    private RelativeLayout mBack;
//    @Override
//    protected void onCreate(Bundle savedInstanceState)
//    {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_home);
//        setActionBarTitle();
//        isReconnect();
//    }
//    /**     * 设置 actionbar 事件     */
//    private void setActionBarTitle() {
//
//        mBack = (RelativeLayout) findViewById(R.id.back);
//
//        mBack.setOnClickListener(new View.OnClickListener()
//        {
//            @Override
//            public void onClick(View v) {
//                finish();
//            }
//        });
//    }
//    /**     * 判断消息是否是 push 消息     */
//    private void isReconnect() {
//        Intent intent = getIntent();
//        String  token = null;
////        if (DemoContext.getInstance() != null)
////        {
////            token = DemoContext.getInstance().getSharedPreferences().getString("DEMO_TOKEN", "defult");
////        }
//        //push或通知过来
//        if (intent != null && intent.getData() != null && intent.getData().getScheme().equals("rong"))
//        {
//            //通过intent.getData().getQueryParameter("push") 为true，判断是否是push消息
//            if (intent.getData().getQueryParameter("push") != null && intent.getData().getQueryParameter("push").equals("true"))
//            {
//                reconnect(token);
//            }
//            else {
//                //程序切到后台，收到消息后点击进入,会执行这里
//                  if (RongIM.getInstance() == null || RongIM.getInstance().getRongIMClient() == null)
//                  {
//                      reconnect(token);
//                  } else
//                      {
//                          enterFragment();
//                      }
//            }
//        }
//    }
//    /**     * 加载 会话列表 ConversationListFragment     */
//    private void enterFragment() {
//        ConversationListFragment fragment = (ConversationListFragment) getSupportFragmentManager().findFragmentById(R.id.fragment2);
//        Uri uri = Uri.parse("rong://" + getApplicationInfo().packageName).buildUpon() .appendPath("conversationlist")
//                .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false") //设置私聊会话是否聚合显示
//             .appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "true")
//                .appendQueryParameter(Conversation.ConversationType.DISCUSSION.getName(), "true")
//                .appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "true")
//                .build();        fragment.setUri(uri);
//    }
//    /**     * 重连     *     * @param token     */
//    private void reconnect(String token) {
//        if ("io.rong.fast".equals(App.getCurProcessName(getApplicationContext()))) {
//            RongIM.connect(token, new RongIMClient.ConnectCallback()
//            {
//                @Override
//                public void onTokenIncorrect()
//                {
//
//                }
//                @Override
//                public void onSuccess(String s) {
//                    enterFragment();
//                }
//                @Override
//                public void onError(RongIMClient.ErrorCode errorCode)
//                {
//
//                }
//            });
//        }
//    }
//}